<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Meta
{
	var $obj;
	var $meta_title;
	var $meta_keyword;
	var $meta_description;
	var $js_link 	= array('all'=>array(), 'meta'=>array());
	var $css_link	= array('all'=>array(), 'meta'=>array());
	var $def_css;
	var $def_js;
	var $is_admin;
	var $module;
	
	public function __construct(){
		$this->obj =& get_instance();	
		$this->init();
	}
	
	public function init()
	{
		
		$this->meta_title = $this->obj->config->meta['title'];
		$this->meta_keyword = $this->obj->config->meta['keyword'];
		$this->meta_description = $this->obj->config->meta['description'];
	}
	
	function link_repair($file)
	{
		$file = str_replace(_ROOT, '', $file);
		$file = str_replace(_URL, '', $file);
		$real_file = preg_replace('~(\?.*?)?$~is', '', $file);
		
		if( is_url($file) ) { 
			return $file;
		}elseif(is_file(_ROOT.$real_file)){
			return _URL.$file;
		}else{
			return false;
		}
	}
	
	function css( $css, $is_meta = TRUE )
	{
		$css = $this->link_repair($css);
		if($css)
		{
			if(!in_array($css, $this->css_link['all']))
			{
				$this->css_link['all'][] = $css;
				if($is_meta) {
					$this->css_link['meta'][] = $css;
				}else{
					echo '<link href="'.$css.'" rel="stylesheet" type="text/css" />', "\n";
				}
			}
		}
	}
	
	function js($js, $is_meta = TRUE )
	{
		$js = $this->link_repair($js);
		if($js)
		{
			if(!in_array($js, $this->js_link['all']))
			{
				$this->js_link['all'][] = $js;
				if($is_meta) {
					$this->js_link['meta'][] = $js;
				}else{
					echo '<script src="'.$js. '" type="text/javascript"></script>', "\n";
				}
			}
		}
	}
	
	public function display( $meta = true )
	{
		
		$output  = "";
		
		$title = $this->meta_title;
		if($this->is_admin){
			$title = $this->meta_title." - Content Management System";	
		}
		
		$output .= "<title>".$title."</title>\n";
		$output .= "<meta http-equiv=\"Pragma\" content=\"no-cache\">\n";
		$output .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
		$output .= "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
		
		if( $meta )
		{
			
			
			
			$output .= meta("verify-v1", session_id());
			$output .= meta("Copyright",COPYRIGHT_OWNER);
			$output .= meta("Author",_URL);
			$output .= meta("description",$this->meta_description);
			$output .= meta("keyword",$this->meta_keyword);
			$output .= meta("revisit-after","2 days");
			$output .= meta("robots","all,index,follow");		
			
			$og_title 		= $this->obj->config->meta['og']['title'];
			$og_site_name 	= $this->obj->config->meta['og']['site_name'];
			$og_url 		= $this->obj->config->meta['og']['url'];
			$og_type 		= $this->obj->config->meta['og']['type'];
			$og_description = $this->obj->config->meta['og']['description'];
			$og_image 		= $this->obj->config->meta['og']['image'];		
			
			# META OG
			$output .= "<meta property=\"og:title\" content=\"$og_title\"/>\n";
			$output .= "<meta property=\"og:site_name\" content=\"$og_site_name\"/>\n";
			$output .= "<meta property=\"og:url\" content=\"$og_url\"/>\n";
			$output .= "<meta property=\"og:type\" content=\"$og_type\"/>\n";
			$output .= "<meta property=\"og:image\" content=\"$og_image\"/>\n";
			$output .= "<meta property=\"og:description\" content=\"$og_description\"/>\n";
		
		}
		$title = $this->meta_title;
		$output .= "<link href=\""._URL."files/logo/".$this->obj->config->meta['favicon']."\" rel=\"shortcut icon\" type=\"image/x-icon\" />\n";
		$output .= "<link href=\"".site_url('content/rss')."\" rel=\"alternate\" type=\"application/rss+xml\" title=\"".$title."\" />\n";
		
		
		
		$arrcss = array_unique(array_merge(array($this->def_css), $this->css_link['meta']));
		foreach((array)$arrcss AS $css){
			if(!empty($css)){
				$output .= "<link href=\"".$css."\" rel=\"stylesheet\" type=\"text/css\" />\n";
			}
		}
		
		$arrjs = array_unique(array_merge(array($this->def_js), $this->js_link['meta']));
		foreach((array)$arrjs AS $js){
			if(!empty($js)){
				$output .= "<script src=\"".$js."\" type=\"text/javascript\"></script>\n";
			}
		}
		
		return $output;
	}
	
	public function set_meta( $meta )
	{
		
		if( !empty($meta) )
		{
			$this->meta_title 		= $meta['title'];
			$this->meta_keyword 	= $meta['keyword'];
			$this->meta_description = $meta['description'];
			
			$this->obj->config->meta['og']['title'] 		= $meta['title'];
			$this->obj->config->meta['og']['url'] 			= $meta['url'];
			$this->obj->config->meta['og']['type'] 			= $meta['type'];
			$this->obj->config->meta['og']['description'] 	= $meta['description'];
			$this->obj->config->meta['og']['image'] 		= $meta['image'];
		}
		
		
	}

	
	public function array_menu( $id = 0, $position = 'top-middle' )
	{
		$return = array();
		$query = "	SELECT id,parent_id,title,url,seo, is_content, content_id FROM sys_menu 
					WHERE parent_id = $id 
					AND is_admin = 0 
					AND position = '$position'
					AND active = 1";
		$query = $this->obj->db_cache->dbQueryCache($query,"all");
		if( count($query) > 0 )
		{
			foreach((array)$query as $row)
			{
				if($row['is_content']==1)
				{
					$content = $this->getContent($row['content_id']);
					$row['url'] = (!empty($row['seo']))?_URL."content/detail/".$row['seo']:_URL."content/detail/$row[content_id]/".url_title($content['title']);	
				}else{
					if( !preg_match("#http\:\/\/#is",$row['url']) )
					{
						$row['url'] = _URL . $row['url'];
					}
				}
				
				$item['id'] 		= $row['id'];
				$item['title'] 		= $row['title'];
				$item['parent_id'] 	= $row['parent_id'];
				$item['url'] 		= $row['url'];
				$item['seo'] 		= $row['seo'];
				$item['child'] 		= $this->array_menu($row['id']);
				$return[] = $item;
			}
		}
		return $return;
	}
	
	public function getContent($id)
	{
		return $this->obj->db_cache->dbQueryCache("SELECT * FROM content WHERE type_id = 1 AND id = $id","row");	
	}
	
	public function bootstrap_menu( $arr_data, $deep = 1)
	{
		$ul_class = ($deep==1)?' class="nav"' : '';
		$html  = "";
		$html .= "<ul$ul_class>\n";	
		
		if( count($arr_data) > 0 )
		{
			foreach((array)$arr_data as $menu)
			{
				$have_child = ( count($menu['child']) > 0 )?true:false;
				$li_class = ($have_child)?' class="menuparent"' : '';
				$html .= "<li$li_class>\n";
					if($have_child){
						$html .= "<span class=\"menuparent nolink\">$menu[title]</span>\n";	
					}else{
						$html .= "<a href=\"$menu[url]\">$menu[title]</a>\n";
					}
					
					if( $have_child )
					{
						$html .= $this->bootstrap_menu($menu['child'],$deep+1);
					}
				$html .= "</li>\n";
			}
		}
		
		$html .= "</ul>\n";
		return $html;
	}
}